﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Dycox.ComponentModel
{
    public abstract class InitializableObject : ISupportInitialize
    {
        protected abstract void BeginInit();

        protected abstract void EndInit();

        private bool _initializing;

        protected bool Initializing
        {
            get { return _initializing; }
        }

        void ISupportInitialize.BeginInit()
        {
            if (_initializing)
                throw new InvalidOperationException("This object is currently initializing.");

            _initializing = true;
            this.BeginInit();
        }

        void ISupportInitialize.EndInit()
        {
            if (!_initializing)
                throw new InvalidOperationException("This object is not currently initializing.");

            this.EndInit();
        }

        protected void ThrowIfNotInitializing()
        {
            if (!_initializing)
                throw new InvalidOperationException("This operation is not allowed after the object is initialized.");
        }
    }
}
